﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class ChatbotLearningQuestionDomainService : IChatbotLearningQuestionDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, ChatbotLearningQuestion> _repository;

        public ChatbotLearningQuestionDomainService(IRepository<Guid, ChatbotLearningQuestion> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public ChatbotLearningQuestion Create(ChatbotLearningQuestion question)
        {
            return _repository.Create(question);
        }

        public ChatbotLearningQuestion Get(Guid id, string include="")
        {
            var result = _repository.Get(id);
            if (result != null && !string.IsNullOrEmpty(include) && include.Split(',').Contains("intent"))
            {
                if (result.TopScoreIntentId.HasValue && result.TopScoreIntentId != Guid.Empty)
                {
                    IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                    result.Intent = intentDomainService.Get(result.TopScoreIntentId.Value, include: "");
                }
            }
            return result;
        }               

        public int Count(ChatbotLearningQuestionFilterSpecification spec)
        {
            return _repository.Count(spec);
        }
        public IEnumerable<ChatbotLearningQuestion> List(ChatbotLearningQuestionFilterSpecification spec, string include = "")
        {
            var list = _repository.List(spec);
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include) && include.Split(',').Contains("intent"))
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                foreach (var item in list)
                {
                    if (item.TopScoreIntentId.HasValue && item.TopScoreIntentId != Guid.Empty)
                    {
                        item.Intent = intentDomainService.Get(item.TopScoreIntentId.Value, include: "");
                    }
                }
            }
            return list;
        }

        public void Delete(Guid id)
        {
            if (_repository.Exists(id))
            {
                ChatbotLearningQuestion learningQuestion = _repository.Get(id);

                _repository.Delete(learningQuestion);
            }
        }
    }
}
